﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MyHelper
{
    public class ReflectionHelper
    {
        #region 得到类的属性的名字
        /// <summary>
        /// 得到类型属性的名字
        /// </summary>
        /// <typeparam name="T">属性名字的集合</typeparam>
        /// <returns></returns>
        public static IEnumerable<string> GetPropertyName<T>() where T : class
        {
            List<string> propertyNameList = new List<string>();
            PropertyInfo[] pis = typeof(T).GetProperties();
            foreach (var item in pis)
            {
                propertyNameList.Add(item.Name);
            }
            return propertyNameList;
        } 
        #endregion
        
        public static IEnumerable<string> GetPropertyType<T>()
        {
            List<string> propertyTypeList = new List<string>();
            PropertyInfo[] pis = typeof(T).GetProperties();
            foreach (var item in pis)
            {
                propertyTypeList.Add(item.GetType().ToString());
            }
            return propertyTypeList;
        }

        #region 实体转化为DataTable
        /// <summary>
        /// 将实体类集合转化为datetable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns>datatable</returns>
        public static DataTable GetTableByEntity<T>(IEnumerable<T> models,bool Second=true) where T : class
        {
            DataTable dt = new DataTable();
            //设置表头
            System.Reflection.PropertyInfo[] pros = typeof(T).GetProperties();
            foreach (System.Reflection.PropertyInfo pi in pros)
            {

                try
                {
                    dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
                }
                catch (Exception)
                {
                    continue;
                }
                
            }
            //通过两种不同的方式像表格中添加数据
            if (Second)
            {
                //逐行加入数据
                foreach (T item in models)
                {
                    DataRow row = dt.NewRow();
                    foreach (var pi in pros)
                    {
                        object propertyValue = pi.GetValue(item, null);
                        if(propertyValue!=null)
                            row[pi.Name] = pi.GetValue(item, null);
                    }
                    dt.Rows.Add(row);
                }
            }
            else
            {
                //逐行加入数据
                foreach (T item in models)
                {
                    object[] itm = new object[pros.Length];//保存每个属性的值
                    int index = 0;//表示要放到object的位置
                    foreach (var pi in pros)
                    {
                        itm.SetValue(pi.GetValue(item, null), index);
                        index++;//数组下标依次加一
                    }

                    //将obect放到dt的row中
                    dt.LoadDataRow(itm, true);
                }
            }
            //返回
            return dt;
            ////将DateTable放入DataSet
            //RDS.Tables.Add(TempDT);
            ////返回DataSet

        } 
        #endregion
       
        #region 根据datatable得到实体集合 add by dufei
        private static IEnumerable<T> EntityListGet<T>(DataTable table) where T : class
        {
            List<T> entityList = new List<T>();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow dr in table.Rows)
                {
                    T entity = (T)Activator.CreateInstance(typeof(T));
                    for (int i = 0; i < dr.Table.Columns.Count; i++)
                    {
                        PropertyInfo propertyInfo = entity.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                        if (propertyInfo != null && dr[i] != DBNull.Value)
                        {
                            propertyInfo.SetValue(entity, dr[i], null);
                        }
                    }
                    entityList.Add(entity);
                }
            }
            return entityList;
        }

        //private static IEnumerable<T> GetEntityListByTable<T>(DataTable dt) where T : class
        //{
        //    PropertyInfo[] pis = typeof(T).GetProperties();
        //    var query=
        //        from p in dt.AsEnumerable()
        //        select new 
        //        {
                    
        //        }
        //}
        #endregion

    }
    
}
